/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.coinche.client.business.handler.impl;

import org.jb.coinche.client.business.message.NextRoundMessage;
import org.jb.coinche.client.business.model.Player;
import org.jb.coinche.client.business.model.RoundFinished;
import org.jb.coinche.client.gin.Global;
import org.jb.coinche.client.ui.card.ScorePanel;
import org.jb.coinche.client.ui.card.StatusPanel;
import org.jb.statemachine.client.GUIStateHandler;
import org.jb.statemachine.client.MessageSender;
import org.jb.user.client.UserLogin;

import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.inject.Inject;

/**
 * Handler pour l'etat "partie finie", on montre le score
 * @author jeremie
 *
 */
public class GUIRoundFinishedHandler implements GUIStateHandler<RoundFinished> {

	@Inject
	private MessageSender sender;
	
	@Inject
	private StatusPanel statusPanel;
	
	@Inject @Global
	private ScorePanel scorePanel;
	
	@Inject @UserLogin
	private String user;
	
	private MessageBox alert;

	/*
	 * (non-Javadoc)
	 * @see org.jb.statemachine.client.GUIStateHandler#disable()
	 */
	@Override
	public void disable() {
		if (alert != null) {
			alert.close();
			alert = null;
		}
		
	}

	/*
	 * (non-Javadoc)
	 * @see org.jb.statemachine.client.GUIStateHandler#enable()
	 */
	@Override
	public void enable() {}

	/*
	 * (non-Javadoc)
	 * @see org.jb.statemachine.client.GUIStateHandler#update(org.jb.statemachine.client.State)
	 */
	@Override
	public void update(RoundFinished state) {

		if (alert != null)
			alert.close();
		
		alert = new MessageBox();
		alert.setTitle("Partie finie");
		
		final boolean viewer = state.getGame().getPlayer( user ) == null;
		
		StringBuilder sb = new StringBuilder();
		
		if (state.getWinner() == null) {
			sb.append("Partie annulée");
		} else {
			sb.append("Score de T1: ");
			sb.append( state.getTeam1Score() );
			sb.append( "<br>" );
			sb.append("Score de T2: ");
			sb.append( state.getTeam2Score() );
			sb.append( "<br><br>" );
			sb.append("Contrat: " );
			sb.append( state.getAuction().getTeam().toString() );
			sb.append( " " );
			sb.append( state.getAuction().getValue() );
			sb.append(" ");
			sb.append( state.getAuction().getTrump().toText() );
			switch( state.getAuction().getCoincheMode() ) {
			case COINCHE:
				sb.append(" coinché");
				break;
			case SURCOINCHE:
				sb.append(" surcoinché");
				break;
			}
			sb.append("<br><br>" );
			sb.append( "Equipe gagnante : (" );
			sb.append( state.getWinner().toString() );
			sb.append( ") " );
			for( Player p : state.getGame().getPlayers() ) {
				if (p.getTeam().equals( state.getWinner())) {
					sb.append( p.getName() );
					sb.append( " ");
				}
			}
			sb.append( "<br><br>" );
			sb.append("T1 gagne " );
			sb.append( state.getTeam1ScoreInc() );
			sb.append(" points<br>");
			sb.append("T2 gagne " );
			sb.append( state.getTeam2ScoreInc() );
			sb.append( " points<br>" );
		}
		
		statusPanel.setStatus("Partie terminée, équipe gagnante : " + state.getWinner().toString() );
		
		scorePanel.setScore( state.getGame().getScore1(), state.getGame().getScore2() );
		
		alert.setMessage( sb.toString() );
	    alert.addCallback(new Listener<MessageBoxEvent>() {
	    	/*
	    	 * 
	    	 */
			@Override
			public void handleEvent(MessageBoxEvent be) {
				if (be.getButtonClicked() != null && ! viewer)
					sender.send( new NextRoundMessage() );
			}
			
		} );
	    alert.setButtons(MessageBox.OK);
	    alert.setIcon(MessageBox.INFO);
		alert.setModal(false);
		alert.setClosable(false);
		alert.show();
		
	}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.guistatemachine.GUIStateHandler#getStateClass()
	 */
	@Override
	public Class<RoundFinished> getStateClass() {
		return RoundFinished.class;
	}

}
